package com.platform.controller;

import java.util.List;
import java.util.Map;

import com.platform.entity.UserEntity;
import com.platform.service.UserService;
import com.platform.utils.MU.MessagePushUtils;
import com.sun.org.apache.regexp.internal.RE;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.platform.entity.MessageEntity;
import com.platform.service.MessageService;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;

/**
 * 消息通知表Controller
 *
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2019-02-16 18:49:49
 */
@RestController
@RequestMapping("/message")
public class MessageController {
    @Autowired
    private MessageService messageService;
    @Autowired
    private UserService userService;
//    @Autowired
//    private MessagePushUtils messagePushUtils;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("message:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<MessageEntity> tMessageList = messageService.queryList(query);
        int total = messageService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(tMessageList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("message:info")
    public R info(@PathVariable("id") Long id) {
        MessageEntity tMessage = messageService.queryObject(id);

        return R.ok().put("message", tMessage);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("message:save")
    public R save(@RequestBody MessageEntity tMessage) {
        messageService.save(tMessage);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("message:update")
    public R update(@RequestBody MessageEntity tMessage) {
        messageService.update(tMessage);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("message:delete")
    public R delete(@RequestBody Long[] ids) {
        messageService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<MessageEntity> list = messageService.queryList(params);

        return R.ok().put("list", list);
    }
    private String ios_appKey = "5c42fd7fb465f5af2b000e58";
    private String ios_appMasterSecret = "lbwbq8scg7vrnunwadxp6d6dumlvnrq4";
    private String and_appKey = "5c42fd5ff1f556dc3b00026a";
    private String and_appMasterSecret = "qif3exedscyjuf8fha0t1l5zeyfzusth";

    @RequestMapping("/pushMess")
    public R pushMessage(@RequestParam("id")Integer id){
        try {
            MessageEntity messageEntity = messageService.queryObject(id.longValue());
            MessagePushUtils android = new MessagePushUtils(and_appKey,and_appMasterSecret);
            boolean afalg = android.sendAndroidBroadcast(messageEntity.getTitle(),messageEntity.getTitle(),messageEntity.getDetail());
            System.out.println("android : " +afalg);
            MessagePushUtils ios = new MessagePushUtils(ios_appKey,ios_appMasterSecret);
            boolean iflag = ios.sendIOSBroadcast(messageEntity.getTitle());
            System.out.println("ios : "+iflag);
            return R.ok();
        }catch (Exception e){
            return R.error();
        }
    }

    /**
     * 单人推送
     * @return
     */
    @RequestMapping("/pushToOne")
    public R pushToOne(@RequestParam("id")Integer id,@RequestParam("phone")String phone){
        try {
            UserEntity userEntity = userService.queryObjectByPhone(phone);
            if(userEntity==null){
                return R.error("未找到用户信息!");
            }
            if(userEntity.getDeviceType()==null){
                return R.error("无绑定设备信息!");
            }
            if(userEntity.getDeviceTokens()==null){
                return R.error("无绑定设备信息!");
            }
            MessageEntity messageEntity = messageService.queryObject(id.longValue());
            if(userEntity.getDeviceType().equals(1)){//安卓
                MessagePushUtils android = new MessagePushUtils(and_appKey,and_appMasterSecret);
                boolean and = android.sendAndroidUnicast(userEntity.getDeviceTokens(),messageEntity.getTitle(),messageEntity.getTitle(),messageEntity.getDetail());
                System.out.println("and : "+and);
                return R.ok();
            }else if(userEntity.getDeviceType().equals(2)){//IOS
                MessagePushUtils ios = new MessagePushUtils(ios_appKey,ios_appMasterSecret);
                boolean os = ios.sendIOSUnicast(userEntity.getDeviceTokens(),messageEntity.getTitle());
                System.out.println("ios : "+os);
                return R.ok();
            }else{
                return R.error("绑定设备信息错误!");
            }
        }catch (Exception e){
            return R.error();
        }
    }
}
